package com.cysyz.web.modules.base.validate.impl;

import com.cysyz.mylib.dao.Conds;
import com.cysyz.web.modules.base.support.service.BaseService;
import com.cysyz.web.modules.base.user.domain.SysUser;
import com.cysyz.web.modules.base.validate.ColumnAndValue;
import com.cysyz.web.modules.base.validate.EntityInfoValidateService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by cuiyong on 15/5/15.
 */
@Service
public class EntityInfoValidateServiceImpl extends BaseService implements EntityInfoValidateService {

    /**
     * 验证实体类的某些属性是否已经存在
     * @param clazz 实体类
     * @param columnAndValues 列名称与其值的数组
     * @return 如果存在返回true，否则返回false
     */
    @Override
    public boolean validateEntityExits(Class clazz, ColumnAndValue... columnAndValues) {
        if (columnAndValues != null) {
            Conds conds = new Conds();
            for (ColumnAndValue columnAndValue : columnAndValues) {
                conds.addFilter(columnAndValue.getColumnName(), columnAndValue.getColumnValue());
            }

            List<SysUser> list = this.list(clazz, conds);

            if (list != null && !list.isEmpty()) {
                return true;
            }
        }
        return false;
    }

}
